package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.CaipuFuliao;
import cn.hellochaos.generator.entity.CaipuList;
import cn.hellochaos.generator.entity.CaipuSummary;
import cn.hellochaos.generator.entity.CaipuZhuliao;
import cn.hellochaos.generator.entity.dto.CaipuDetailDto;
import cn.hellochaos.generator.mapper.CaipuListMapper;
import cn.hellochaos.generator.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* <p>
* 主表：菜谱列表 服务实现类
* </p>
*
* @author test
* @since 2021-01-23
*/
@Slf4j
@Service
public class CaipuListServiceImpl extends ServiceImpl<CaipuListMapper, CaipuList> implements CaipuListService {

    @Autowired
    CaipuListMapper caipuListMapper;

    @Autowired
    CaipuSummaryService caipuSummaryService;

    @Autowired
    CaipuFuliaoService caipuFuliaoService;

    @Autowired
    CaipuBuzhouService caipuBuzhouService;

    @Autowired
    CaipuZhuliaoService caipuZhuliaoService;

    @Autowired
    CaipuTipsService caipuTipsService;

    /**
     * TODO
     * 假数据，业务功能未实现！
     * @return
     */
    @Override
    public CaipuDetailDto getCaipuDetailById(Integer caipuBookId) {

        CaipuDetailDto caipuDetailDto = new CaipuDetailDto();

        QueryWrapper<CaipuList> queryWrapper = new QueryWrapper<CaipuList>();
        queryWrapper.eq("id_cookbook",caipuBookId);
        List<CaipuList> caipuLists = caipuListMapper.selectList(queryWrapper);
        CaipuList caipuList = caipuLists.get(0);
        caipuDetailDto.setCaipuList(caipuList);

        CaipuSummary caipuSummary = caipuSummaryService.getCaipuSummaryByCookbookId(caipuBookId);
        caipuDetailDto.setCaipuSummary(caipuSummary);

        List<CaipuFuliao> caipuFuliaos= caipuFuliaoService.getCaipuBuzhouByCookBookId(caipuBookId);
        caipuDetailDto.setCaipuFuLiao(caipuFuliaos);

        caipuDetailDto.setCaipuBuZhous(caipuBuzhouService.getCaipuBuzhouByCookBookId(caipuBookId));

        caipuDetailDto.setCaipuZhuLiao(caipuZhuliaoService.getCaipuBuzhouByCookBookId(caipuBookId));
        caipuDetailDto.setCaipuTips(caipuTipsService.getCaipuTipByCookBookId(caipuBookId));

        return caipuDetailDto;
    }

    /**
     * TODO
     * 假数据，业务功能未实现！
     * @return
     */
    @Override
    public List<CaipuList> findNewList7() {

        log.info("从数据表中随机抽取7条数据");

        QueryWrapper<CaipuList> queryWrapper = new QueryWrapper<CaipuList>();
        queryWrapper.orderByDesc("create_time");
//        List<CaipuList> caipuLists = caipuListMapper.selectList(queryWrapper);
        List<CaipuList> caipuLists = caipuListMapper.getCaipuRand7();

        return caipuLists;
    }


    /**
     * TODO
     * 该方法返回的为假数据
     * @param cookbookName
     * @return
     */
    @Override
    public List<CaipuList> getCookbookByCookBookName(String cookbookName) {

        List<CaipuList> caipuList = new ArrayList<CaipuList>();
//        caipuDetailDto.setCaipuList();
        for (int i = 0; i < 6; i++) {
            caipuList.add(i,null);
        }
        return caipuList;
    }

    /*===========================================================================*/

    @Override
    public Page<CaipuList> listCaipuListsByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询caipuList: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<CaipuList> queryWrapper =  new QueryWrapper<CaipuList>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<CaipuList> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询caipuList完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public CaipuList getCaipuListById(int id) {
        log.info("正在查询caipuList中id为{}的数据",id);
        CaipuList caipuList = super.getById(id);
        log.info("查询id为{}的caipuList{}",id,(null == caipuList?"无结果":"成功"));
        return caipuList;
    }

    @Override
    public int insertCaipuList(CaipuList caipuList) {
        log.info("正在插入caipuList");
        if (super.save(caipuList)) {
            log.info("插入caipuList成功,id为{}",caipuList.getId());
            return caipuList.getId();
        } else {
            log.error("插入caipuList失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteCaipuListById(int id) {
        log.info("正在删除id为{}的caipuList",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的caipuList成功",id);
            return id;
        } else {
            log.error("删除id为{}的caipuList失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateCaipuList(CaipuList caipuList) {
        log.info("正在更新id为{}的caipuList",caipuList.getId());
        if (super.updateById(caipuList)) {
            log.info("更新d为{}的caipuList成功",caipuList.getId());
            return caipuList.getId();
        } else {
            log.error("更新id为{}的caipuList失败",caipuList.getId());
            throw new BizException("更新失败[id=" + caipuList.getId() + "]");
        }
    }


}
